<?php
namespace Dashboard\Controller;

use Think\Controller;
use Think\Session\Driver\Memcache;

class CouponsController extends BaseController{
    //首页
    public function index(){
        $a = (int)($_POST["currpage"]);//跳转页数
        if($a != null){
            $currpage = $a;
        }else{
            $currpage = isset($_GET['currpage'])?$_GET['currpage']:1;
        }
        $totalpage = 0;//总页数
        $pagesize  = 10;//每页显示数
        //搜索
        $sou     = $_POST['sou'];
        $content = $_POST['keywords'];
        if($sou != null){ //标题
            $currpage  = 1;
            $totalpage = 1;//总页数
            if($sou == 1){$list      = sels("coupons","name like '%{$content}%'");}
            if($sou == 2){$list      = sels("coupons","phone like '%{$content}%'");}
        }else{
            $arr       = currpage("coupons","","$pagesize","$currpage");
            $totalpage = $arr['totalpage'];
            $list      = $arr['list'];
        }
        foreach ($list as $k=>$v){
            $list[$k]['start_at'] = date("Y-m-d",$v['start_at']);
            $list[$k]['end_at']   = date("Y-m-d",$v['end_at']);
        }
        $this->assign("list",$list)->assign("currpage",$currpage)->assign("totalpage",$totalpage);
        $this->display();
    }
    public function form(){
        $id      = I('id');
        if($id != null){
            $list = sel("coupons","",array("id"=>$id));
            $list['start_at']    = date("Y-m-d",$list['start_at']);
            $list['end_at']      = date("Y-m-d",$list['end_at']);
            $this->assign("list",$list);
        }
        $this->display();
    }

    //添加/修改
    public function add(){
        $id                   = $_POST['id'];
        $data['title']       = I('title');
        $data['type']        = I('type');
        $data['name']        = I('name');
        $data['address']     = I('address');
        $data['man']         = I('man');
        $data['money']       = I('money');
        $data['phone']       = I('phone');
        $data['start_at']    = strtotime(I('start_at'));
        $data['end_at']      = strtotime(I('end_at'));
        $data['admin_id']    = $_SESSION['ad']['id'];
        if($id == null){
            $info               = sel("coupons","",array("name"=>$data['name']));
            if(!$info){
                $data['create_at'] = time();
                //处理图片
                if($_FILES['one']['name'] != null){
                    $info                   = $this->imgUpload("you/");
                    $savename               = $info['one']['savename'];
                    $savepath               = $info['one']['savepath'];
                    $data['img']            = "Data/".$savepath.$savename;
                }
                $result = ins("coupons",$data);
                if($result){
                    $content = "管理员：".$_SESSION['admin']['numbers']." 于 ".date("Y-m-d H:i:s",time())."添加了名称为：‘".$data['name']."’的优惠券";
                    $log     = ins("logs",array("content"=>$content,"admin_id"=>$_SESSION['admin']['id']));
                    $this->success("添加成功",U('form'));
                }
            }else{
                $this->error("请勿重复添加相同名称的优惠券");
            }
        }else{
            $data['up_at']     = time();
            $img                = I('img');
            //处理图片
            if($_FILES['one']['name'] != null){
                unlink($img);
                $info                   = $this->imgUpload("you/");
                $savename               = $info['one']['savename'];
                $savepath               = $info['one']['savepath'];
                $data['img']            = "Data/".$savepath.$savename;
            }
            $info    = sel("coupons","",array("id"=>$id));
            $content = "管理员：".$_SESSION['admin']['numbers']." 于 ".date("Y-m-d H:i:s",time())."修改了名称为：‘".$info['name']."’的优惠券";

            $result = up_one("coupons",array("id"=>$id),$data);
            if($result){
                $log     = ins("logs",array("content"=>$content,"admin_id"=>$_SESSION['admin']['id']));
                $this->success("修改成功",U('index'));
            }else{$this->success("没有信息变更，修改失败");}
        }
    }
}